from collections import OrderedDict
import matplotlib.pyplot as plt


def plot_dict(correct_dict):
    tree_depth_list = [2, 3, 4, 5, 6]
    plt.figure(figsize=(7, 4.5), dpi=80)
    for m, l in zip(correct_dict, ['o', 'v', '^', 's', '8', '*', 'D', 'H']):
        if m == 'CART':
            plt.plot(tree_depth_list, correct_dict[m], marker=l, alpha=.7, label=m)
        else:
            if 'e' in m:
                nmum = m.split('=')[1][:-1]

                plt.plot(tree_depth_list, correct_dict[m], marker=l, alpha=.7, label=r'%s($\epsilon=%s$)' % (m[:11], nmum))
            else:
                plt.plot(tree_depth_list, correct_dict[m], marker=l, alpha=.7, label=m)

    plt.legend(loc=4, frameon=False, fontsize=7)
    plt.xlabel('Depth', fontsize=7)
    plt.ylabel('Accuracy(%)', fontsize=7)
    plt.xticks(tree_depth_list, fontsize=7)
    plt.show()

all_5_dict =(
         OrderedDict([
        ('CART', [0.831178204125819, 0.739119103466249, 0.793668932058298, 0.806509492592369, 0.812624045227641]),
        ('AdaBoost', [0.6874862171969287, 0.7115234257532929, 0.767125759307151, 0.7922455443956616, 0.8497624345943345]),
        ('DPsAdaBoost(e=0.05)', [0.6177402217277119, 0.6184318678454722, 0.6622661935406267, 0.6737435095527355, 0.7149114893446402]),
        ('DPsAdaBoost(e=0.1)', [0.6606122571720695, 0.6733525791383492, 0.7308794931938013, 0.7509773260359656, 0.8115314448387162]),
        ('DPsAdaBoost(e=0.25)', [0.6756079469136544, 0.6961769010244382, 0.7611314929532287, 0.7885567651009402, 0.8451915559030493]),
        ('DPsAdaBoost(e=0.5)', [0.6827048375132816, 0.701459473547042, 0.7720374491289268, 0.797648403199615, 0.8479380926605321]),
        ('DPsAdaBoost(e=0.75)', [0.6852108017080651, 0.7031735530562739, 0.774362983901686, 0.798390168601271, 0.8488803351977707]),
        ('DPsAdaBoost(e=1)', [0.6851105631402739, 0.7046069645756902, 0.7732403119424229, 0.7986207173071911, 0.8490206691926786])]),
         OrderedDict(
        [('CART', [0.831178204125819, 0.739119103466249, 0.793668932058298, 0.806509492592369, 0.812624045227641]),
        ('AdaBoost', [0.748170646137808, 0.756079469136545, 0.716896212986908, 0.77083458631543, 0.805998275896634]),
        ('DPsAdaBoost(e=0.05)',[0.6139912992923157, 0.6220504801427397, 0.6662757362522804, 0.6717186904833504, 0.7146007497844871]),
        ('DPsAdaBoost(e=0.1)',[0.6620857641186023, 0.6741143922535635, 0.7314308053166536, 0.752420761412161, 0.8105290591608028]),
        ('DPsAdaBoost(e=0.25)',[0.6768709528678254, 0.6956155650448066, 0.7610513020989956, 0.7891982919348048, 0.8439586215192157]),
        ('DPsAdaBoost(e=0.5)',[0.6833964836310419, 0.7019606663859986, 0.7725987851085584, 0.7972474489284497, 0.8481987129367896]),
        ('DPsAdaBoost(e=0.75)',[0.6853812072733105, 0.7032838154808444, 0.7738317194923919, 0.7987911228724364, 0.8484392854994888]),
        ('DPsAdaBoost(e=1)',[0.6854814458411018, 0.7049577995629599, 0.7728092861009201, 0.7990116477215774, 0.8487500250596419])]),
         OrderedDict(
        [('CART', [0.831178204125819, 0.739119103466249, 0.793668932058298, 0.806509492592369, 0.812624045227641]),
        ('AdaBoost', [0.6874862171969287, 0.7115234257532929, 0.767125759307151, 0.7922455443956616, 0.8497624345943345]),
        ('DPsAdaBoost(e=0.05)', [0.6174294821675588, 0.6220304324291814, 0.662737314809246, 0.6716785950562338, 0.7180088610893928]),
        ('DPsAdaBoost(e=0.1)', [0.6611836170084802, 0.6746456566628576, 0.732202642288647, 0.7538942683586937, 0.8126039975140835]),
        ('DPsAdaBoost(e=0.25)', [0.6760089011848198, 0.6968184278583027, 0.7619534492091177, 0.7884064072492533, 0.8440789078005654]),
        ('DPsAdaBoost(e=0.5)', [0.6833664120607045, 0.7019406186724404, 0.7725586896814418, 0.7968765662276217, 0.8482388083639061]),
        ('DPsAdaBoost(e=0.75)', [0.6857621138309176, 0.7024819069385136, 0.7739620296305206, 0.7993725065656262, 0.8485395240672802]),
        ('DPsAdaBoost(e=1)', [0.6851707062809487, 0.7043162727290952, 0.7731100018042942, 0.799883723261362, 0.8486898819189671])]),
         OrderedDict(
        [('CART', [0.831178204125819, 0.739119103466249, 0.793668932058298, 0.806509492592369, 0.812624045227641]),
        ('AdaBoost', [0.6874862171969287, 0.7115234257532929, 0.767125759307151, 0.7922455443956616, 0.8497624345943345]),
        ('DPsAdaBoost(e=0.05)', [0.6146228022694011, 0.6202862813496121, 0.6651931597201339, 0.6733425552815702, 0.7186403640664782]),
        ('DPsAdaBoost(e=0.1)', [0.6616647621338786, 0.6747960145145446, 0.7298370120887713, 0.7523004751308113, 0.8115915879793909]),
        ('DPsAdaBoost(e=0.25)', [0.6754375413484092, 0.6968384755718611, 0.7617830436438724, 0.7898698903390068, 0.844800625488663]),
        ('DPsAdaBoost(e=0.5)', [0.682584551231932, 0.7018002846775325, 0.7727391191034663, 0.7975180930614864, 0.8479080210901947]),
        ('DPsAdaBoost(e=0.75)', [0.6848599667207955, 0.7027525510715503, 0.7746536757482809, 0.7991319340029269, 0.8492111224714821]),
        ('DPsAdaBoost(e=1)', [0.6857119945470219, 0.7040957478799543, 0.7733205027966561, 0.7993123634249514, 0.8485796194943966])]),
         OrderedDict(
        [('CART', [0.8421242557286341, 0.7292856999659189, 0.7916641607024719, 0.8013772779214531, 0.8100880094625208]),
        ('AdaBoost', [0.6874862171969287, 0.7115234257532929, 0.767125759307151, 0.7922455443956616, 0.8497624345943345]),
        ('DPsAdaBoost(e=0.05)', [0.6161664762133878, 0.6196948737996432, 0.6629377919448287, 0.6730819350053127, 0.7161544475852529]),
        ('DPsAdaBoost(e=0.1)', [0.6581463884044024, 0.6740141536857721, 0.7288346264108578, 0.7520599025681121, 0.8109300134319681]),
        ('DPsAdaBoost(e=0.25)', [0.6744952988111705, 0.6967081654337323, 0.7619233776387803, 0.7883462641085784, 0.8443595757903811]),
        ('DPsAdaBoost(e=0.5)', [0.6825544796615945, 0.7016198552555081, 0.7725486658246628, 0.7971873057877749, 0.8479280688037529]),
        ('DPsAdaBoost(e=0.75)', [0.6853511357029731, 0.7032136484833905, 0.7744832701830356, 0.7994226258495218, 0.8483089753613601]),
        ('DPsAdaBoost(e=1)', [0.6848699905775746, 0.7049177041358433, 0.7732503357992021, 0.7991118862893687, 0.8488602874842124])]))


# 计算均值
def avg_cict(*odict):
    avg_o_dict = OrderedDict({})
    c = len(odict)
    d = 0
    f = 0
    for k in odict[d]:
        if f != 0:
            for s in k:
                avg_o_dict[s] += [j / c for j in k[s]]
        else:
            for s in k:
                avg_o_dict[s] = [j / c for j in k[s]]
        d += 1
    return avg_o_dict


plot_dict(avg_cict(all_5_dict))


# ROC的点
roc_dict_10 = OrderedDict(
    [('AdaBoost', [[0, 0.01600387972841901, 0.016327190430003233, 0.016650501131587454, 0.021661817006142902,
                    0.021823472356935016, 0.023278370514064017, 0.024894924021985128, 0.027481409634658907,
                    0.028289686388619464,
                    0.03136113805366958, 0.032654380860006466, 0.03976721629485936, 0.04009052699644358,
                    0.042515357258325254,
                    0.05156805690268348, 0.06256062075654704, 0.06401551891367604, 0.06595538312318137,
                    0.0677335919818946,
                    0.06886517943743937, 0.07549304881991593, 0.0906886517943744, 0.09408341416100872,
                    0.1008729388942774,
                    0.11170384739734886, 0.1181700614290333, 0.12124151309408342, 0.1239896540575493,
                    0.12609117361784675,
                    0.12883931458131265, 0.1498545101842871, 0.15955383123181377, 0.162786938247656,
                    0.16666666666666666,
                    0.17086970578726157, 0.17685095376656967, 0.1859036534109279, 0.19818946007112836,
                    0.23246039443905595, 1, 1.0],
                   [0.5845302214242968, 0.5845302214242968, 0.5854920064973925, 0.5871697871249039, 0.630589039924767,
                    0.6320424040352227, 0.6462127041121655, 0.6546336667521587, 0.6634393434213901, 0.6656621355903223,
                    0.6726617936222963,
                    0.6765089339146789, 0.7091561938958707, 0.7114644780713003, 0.7192763101649996, 0.7302406599982901,
                    0.7674831153287167,
                    0.7739057023168334, 0.7780306916303326, 0.7812793878772335, 0.7825190219714456, 0.7853509446866718,
                    0.806916303325639,
                    0.80893605197914, 0.8177096691459349, 0.8281503804394289, 0.8361225100453108, 0.8400978883474395,
                    0.8428122595537317,
                    0.845718987774643, 0.8466380268444901, 0.8643348721894503, 0.8741771394374626, 0.876902197144567,
                    0.8819034795246644,
                    0.8854407112934941, 0.8882405745062837, 0.8908267077028298, 0.8955928870650595, 0.9117722492946909,
                    1.0, 1.0]]),

     ('DPsAdaBoost(e=0.1)', [[0, 0.05140640155189137, 0.053831231813773035, 0.05706433882961526, 0.060459101196249594,
                              0.06466214031684449, 0.06886517943743937, 0.07306821855803428, 0.07872615583575816, 0.08438409311348205,
                              0.08891044293566117, 0.09456838021338507, 0.09909473003556418, 0.10442935661170384, 0.10830908503071451,
                              0.1152602651147753, 0.1225347559004203, 0.12867765923052052, 0.13498221791141288, 0.14225670869705787,
                              0.14920788878111865, 0.1571290009699321, 0.16456514710636921, 0.17313288069835112, 0.18105399288716456,
                              0.18832848367280958, 0.197704494018752, 0.2074038150662787, 0.21645651471063693, 0.22809569996766893,
                              0.2374717103136114, 0.24894924021985126, 0.2581635952150016, 0.2696411251212415, 0.2809569996766893,
                              0.29178790817976075, 0.3056902683478823, 0.3170061429033301, 0.32913029421273843, 0.3433559650824442, 1, 1.0],
                             [0.5530157305291955, 0.5530157305291955, 0.5641510643754809, 0.5749444301957767, 0.5859087800290673,
                              0.5965418483371805, 0.6075703171753442, 0.6182995639907669, 0.628312815251774, 0.6394481490980594,
                              0.6491514918355134, 0.6591006240916474, 0.6693810378729589, 0.678411131059246, 0.688050354791827,
                              0.6975185945114132, 0.7066341797041976, 0.7158993759083526, 0.7245020090621527, 0.733179447721638,
                              0.7414721723518851, 0.7500427460032487, 0.7579721296058819, 0.7659549457125758, 0.7733393177737882,
                              0.7815679233991621, 0.7891232794733692, 0.7965183380353937, 0.8033683850559973, 0.810539027100966,
                              0.817474566128067, 0.8243032401470463, 0.8309395571514063, 0.8376613661622638, 0.8438061041292639,
                              0.8496088740702744, 0.855539882021031, 0.8612571599555442, 0.8674660169274173, 0.8730764298538086, 1.0, 1.0]]),
     ('DPsAdaBoost(e=0.5)', [
         [0, 0.017782088587132233, 0.0187520206918849, 0.02004526349822179, 0.021823472356935016, 0.02376333656644035,
          0.02554154542515357, 0.026996443582282575, 0.029744584545748465, 0.03200775945683802, 0.03475590042030391,
          0.03847397348852247, 0.042192046556741025, 0.0460717749757517, 0.04962819269317814, 0.053831231813773035,
          0.056417717426446815, 0.059974135143873265, 0.06547041707080505, 0.07032007759456838, 0.07775622373100549,
          0.08260588425476884, 0.0877788554801164, 0.09473003556417717, 0.10006466214031684, 0.10636922082120918,
          0.11364371160685419, 0.1196249595861623, 0.12786938247655996, 0.13514387326220498, 0.14080181053992888,
          0.14678305851923698, 0.15470417070805043, 0.16424183640478499, 0.17523440025864856, 0.18687358551568056,
          0.1973811833171678, 0.2075654704170708, 0.22001293242806336, 0.2335919818946007, 1, 1.0],
         [0.5855454390014534, 0.5855454390014534, 0.5969158758656066, 0.6079229717021458, 0.6184171154997008,
          0.6287295887834488, 0.6394374625972472, 0.6501132769086091, 0.6605326152004788, 0.6715290245362059,
          0.6824826878686843, 0.6936500812174061, 0.7051701290929299, 0.7160383004189108, 0.7267568607335214,
          0.7369197230058989, 0.747114644780713, 0.757063777036847, 0.7667243737710524, 0.7759468239719586,
          0.7845922031290075, 0.7927673762503206, 0.801081473882192, 0.808326921432846, 0.815390698469693,
          0.8224972215097889, 0.8295289390441993, 0.8363896725656151, 0.8430901085748482, 0.8499401555954518,
          0.8559459690518937, 0.8621120800205181, 0.8681392664785843, 0.8742519449431478, 0.8799692228776609,
          0.8861887663503463, 0.8923869368214072, 0.8981149012567325, 0.9039497307001796, 0.9096670086346926, 1.0, 1.0]]),
     ('DPsAdaBoost(e=1)', [
         [0, 0.01600387972841901, 0.0172971225347559, 0.01859036534109279, 0.02004526349822179, 0.021985127707727127,
          0.023116715163271906, 0.025703200775945685, 0.02796637568703524, 0.030067895247332686, 0.032654380860006466,
          0.03507921112188814, 0.03847397348852247, 0.0431619786614937, 0.048334949886841255, 0.052699644358228256,
          0.05706433882961526, 0.061429033301002266, 0.06563207242159716, 0.0706433882961526, 0.07484642741674749,
          0.07969608794051083, 0.08697057872615584, 0.09473003556417717, 0.10103459424506951, 0.10911736178467507,
          0.11542192046556742, 0.1210798577432913, 0.12722276107339153, 0.13174911089557065, 0.13918525703200776,
          0.1454898157129001, 0.15227934044616878, 0.15906886517943744, 0.16650501131587456, 0.17507274490785646,
          0.18574199806013578, 0.19657290656320725, 0.21112188813449725, 0.22664080181053992, 1, 1.0],
         [0.5866140890826708, 0.5866140890826708, 0.5956655552705822, 0.6062665640762589, 0.6181392664785843,
          0.6302150123963409, 0.6418419252799863, 0.65237881508079, 0.6607356587159101, 0.6689322048388475,
          0.6782080875438147, 0.6885419338291869, 0.6997627596819698, 0.7124048901427716, 0.7259874326750448,
          0.7396768402154399, 0.7525327006924852, 0.7634222450200906, 0.7726446952209969, 0.7801252457895187,
          0.7873172608361119, 0.794936735915192, 0.8027806275113277, 0.8104855945969052, 0.8181798751816706,
          0.8259275882704967, 0.832574591775669, 0.8384201077199281, 0.8441373856544413, 0.8500470206035735,
          0.8561062665640763, 0.8626250320595025, 0.868865948533812, 0.8751709840129948, 0.8805783534239549,
          0.8852697272804992, 0.8905274856800889, 0.8962768231170386, 0.9027955886124648, 0.9099769171582457, 1.0, 1.0]])])

# AUC
area_dict_10 = OrderedDict([
    ('AdaBoost', 0.9208064945994232),
    ('DPsAdaBoost(e=0.1)', 0.8672490184243427),
    ('DPsAdaBoost(e=0.5)', 0.9191167896958279),
    ('DPsAdaBoost(e=1)', 0.9204181112963853)])


def plot_roc(roc_dict):
    plt.figure(figsize=(7, 4.5), dpi=80)
    plt.plot([0, 1], [0, 1], '--', c='r')
    for i, m in zip(roc_dict, ['.', 's', '*', 'H']):
        x, y = roc_dict[i][0], roc_dict[i][1]
        # 将x,y换成折线型
        need_x = [0]
        need_y = [0]
        for index, value in enumerate(x[:-3]):
            if y[index + 1] > y[index]:
                need_x.append(value)
                need_y.append(y[index])
                need_x.append(x[index + 1])
                need_y.append(y[index])
            else:
                need_x.append(value)
                need_y.append(y[index])
        need_y += [1]
        need_x += [need_x[-1]]
        need_y += [1]
        need_x += [1]
        if 'e' in i:
            nmum = i.split('=')[1][:-1]

            plt.plot(need_x, need_y, marker=m, ms=.3, alpha=.7, label=r'%s($\epsilon=%s$)' % (i[:11], nmum))
        else:
            plt.plot(need_x, need_y, marker=m, ms=.3, alpha=.7, label=i)

        plt.xlim((0, 1))
        plt.ylim((0, 1))
    plt.legend(loc=4, frameon=False, fontsize=7)
    plt.title('ROC', fontsize=7)
    plt.xlabel('False positive rate', fontsize=7)
    plt.ylabel('True positive rate', fontsize=7)
    plt.show()


plot_roc(roc_dict_10)

